package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.widget.switchbutton.SwitchButton;
import com.hangzhou.xyj.utils.ToastUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;

public class MySettingActivity extends BaseActivity implements View.OnClickListener {
    private static final int DEFAULT_CACHE_SIZE = 32;//默认有32k缓存

    private Button mBtnExit;
    private SwitchButton mSbNofiy;//消息提醒 开关

    @Override
    public void initTop() {
        enableTitle(true, "我的设置");
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_setting);
    }

    @Override
    public void initViews() {
        mBtnExit = findView(R.id.btn_exit);
        mSbNofiy = findView(R.id.sb_message_notify);
        if (XYJApplication.getUserInfo() == null) {
            mBtnExit.setVisibility(View.GONE);
        } else {
            mBtnExit.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void initDatas() {
        mSbNofiy.setChecked(CommonPreferences.getEnablePush(mContext, String.valueOf(mMyUserId)));
    }

    @Override
    public void initListeners() {
        findView(R.id.civ_update_pwd).setOnClickListener(this);
        findView(R.id.civ_clean_cache).setOnClickListener(this);
        findView(R.id.civ_about_us).setOnClickListener(this);
        mBtnExit.setOnClickListener(this);
        mSbNofiy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    CommonPreferences.setEnablePush(mContext, String.valueOf(mMyUserId), true);
                } else {
                    CommonPreferences.setEnablePush(mContext, String.valueOf(mMyUserId), false);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.civ_update_pwd:
                IntentManager.getInstance().goUpdatePasswordActivity(mContext);
                break;
            case R.id.civ_clean_cache:
                clearCache();
                break;
            case R.id.btn_exit:
                XYJApplication.getInstance().exit();
                CommonPreferences.removeKey(mContext, "userId");
//                Intent intent = new Intent(mContext, MainActivity.class);
//                intent.putExtra(DataKey.INTENT_TAG, "cancel");
                IntentManager.getInstance().goMainActivity(mContext, Intent.FLAG_ACTIVITY_CLEAR_TASK);
                closeActivity();
                break;
            case R.id.civ_about_us:
                IntentManager.getInstance().goAboutUs(mContext);
                break;

        }
    }

    /**
     * 清理图片缓存
     */
    private void clearCache() {
        File directory = ImageLoader.getInstance().getDiskCache().getDirectory();
        float folderSize = folderSize(directory);
        String unit = "KB";
        if (folderSize > 1024) {
            unit = "KB";
            folderSize = folderSize % 1024f;
        }

        if (folderSize > 1024) {
            unit = "M";
            folderSize = folderSize % 1024f;
        }

        final float finalFolderSize = folderSize;
        final String finalUnit = unit;
        findView(R.id.civ_clean_cache).post(new Runnable() {
            @Override
            public void run() {
                ImageLoader.getInstance().clearDiskCache();
                String format = "0KB";//默认0KB
                if (finalFolderSize > DEFAULT_CACHE_SIZE) {
                    format = String.format(" %.2f %s", finalFolderSize, finalUnit);
                }
//                ToastUtil.show("已清理" + format + "缓存");
                ToastUtil.show("清理缓存成功");
            }
        });
    }

    /**
     * 计算缓存文件夹大小
     *
     * @param directory
     * @return
     */
    public long folderSize(File directory) {
        long length = 0;
        for (File file : directory.listFiles()) {
            if (file.isFile())
                length += file.length();
            else
                length += folderSize(file);
        }
        return length;
    }
}
